package media.sound;

import com.game.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;

public class SoundBackground {
	private MediaPlayer mp = null;
	private Context m_context = null;
	private  AudioManager  m_audioManager;
	private int m_volume;
	
	public static final int LEVEL1_SOUND = 0;
	
	private static final int SOUND_MAINMENU_ID = R.raw.sound_mainmenu;
	private static final int SOUND_LEVEL1_ID = R.raw.sound_level1;
	private static final int SOUND_BOSS1_ID = R.raw.sound_boss1;
	private static final int SOUND_GAMEOVER_ID = R.raw.sound_gameover;
	private static final int SOUND_WIN_ID = R.raw.sound_win;
	private static final int SOUND_BACKCOUNTRY_ID = R.raw.sound_backcountry;
	
	public SoundBackground(Context context)
	{
		m_context = context;
		m_audioManager = (AudioManager)m_context.getSystemService(Context.AUDIO_SERVICE);
	}
	
	public void Stop()
	{
		if(mp != null)
		{
			if(mp.isPlaying()) mp.stop();
			mp.release();
			mp = null;
		}
	}
	
	public void PlayMainMenu()
	{
		if(mp != null)
		{
			if(mp.isPlaying()) mp.stop();
			mp.release();
		}
		
		mp = MediaPlayer.create(m_context, SOUND_MAINMENU_ID);
		mp.setLooping(true);
		m_volume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mp.setVolume(m_volume, m_volume);
		mp.start();
	}
	
	public void PlayIngame(int level)
	{
		if(mp != null)
		{
			if(mp.isPlaying()) mp.stop();
			mp.release();
		}
		
		switch(level)
		{
		case LEVEL1_SOUND:
			mp = MediaPlayer.create(m_context, SOUND_LEVEL1_ID);
			break;
		}
		
		mp.setLooping(true);
		m_volume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mp.setVolume(m_volume, m_volume);
		mp.start();
	}
	
	public void PlayBoss(int level)
	{
		if(mp != null)
		{
			if(mp.isPlaying()) mp.stop();
			mp.release();
		}
		
		switch(level)
		{
		case LEVEL1_SOUND:
			mp = MediaPlayer.create(m_context, SOUND_BOSS1_ID);
			break;
		}
		
		mp.setLooping(true);
		m_volume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mp.setVolume(m_volume, m_volume);
		mp.start();
	}
	
	public void PlayGameOver()
	{
		if(mp != null)
		{
			if(mp.isPlaying()) mp.stop();
			mp.release();
		}
		
		mp = MediaPlayer.create(m_context, SOUND_GAMEOVER_ID);
		mp.setLooping(false);
		m_volume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mp.setVolume(m_volume, m_volume);
		mp.start();
	}
	
	public void PlayWin()
	{
		if(mp != null)
		{
			if(mp.isPlaying()) mp.stop();
			mp.release();
		}
		
		mp = MediaPlayer.create(m_context, SOUND_WIN_ID);
		mp.setLooping(false);
		m_volume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mp.setVolume(m_volume, m_volume);
		mp.start();
	}
	
	public void PlayBackCountry()
	{
		if(mp != null)
		{
			if(mp.isPlaying()) mp.stop();
			mp.release();
		}
		
		mp = MediaPlayer.create(m_context, SOUND_BACKCOUNTRY_ID);
		mp.setLooping(false);
		m_volume = m_audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		mp.setVolume(m_volume, m_volume);
		mp.start();
	}
}
